Namespace('models.role',{
	init:function(){
		easyui.util.initDatagrid("#role",{
			url:contextPath+"/role/query",
			toolbar:"#toolbar",
			columns:[[
			 {field:'id',title:'id',width:80,hidden:true},    
 			 {field:'name',title:'角色名称',width:100},    
 			 {field:'remark',title:'备注',width:200}    
			]]
		});
	},
	query:function(){
		
	},
	add:function(){
		getDialog("addrole").dialog({
			href:contextPath+'/role/add',
			title: '新增',
		    width: 550,
		    height: 350,
		    buttons:[{
				text:'保存',
				iconCls:'icon-ok',
				handler:function(){
					models.role.doAdd();
				}
				},{
				text:'取消',
				iconCls:'icon-cancel',
				handler:function(){
					$('#addrole').dialog('destroy');
				}
			}]
		});
	},
	doAdd:function(){
		var role = $("#roleform").serialize();
		$.post(contextPath+"/role/add",role,function(data){
			if(data){
				alert("新增角色成功！");
				$('#addrole').dialog('destroy');
				$("#role").datagrid('load');
			}else{
					alert("新增角色失败！");
			}
		});
	},update:function(){
		
		var row =$("#role").datagrid('getSelected');
		if(row.length==0){
			alert("请选择一条数据！");
			return;
		}
		getDialog("updaterole").dialog({
			href:contextPath+'/role/update?id='+row.id,
			title: '新增',
		    width: 550,
		    height: 350,
		    buttons:[{
				text:'保存',
				iconCls:'icon-ok',
				handler:function(){
					models.role.doUpdate();
				}
				},{
				text:'取消',
				iconCls:'icon-cancel',
				handler:function(){
					$('#updaterole').dialog('destroy');
				}
			}]
		});
	},doUpdate:function(){
		var role = $("#roleform").serialize();
		$.post(contextPath+"/role/update",role,function(data){
			if(data){
				alert("修改角色成功！");
				$('#updaterole').dialog('destroy');
				$("#role").datagrid('load');
			}else{
					alert("修改角色失败！");
			}
		});
	},
	remove:function(){
		var row =$("#role").datagrid('getSelected');
		if(row.length==0){
			alert("请选择一条数据！");
			return;
		}
		$.post(contextPath+"/role/remove",{id:row.id},function(data){
			if(data){
				$("#role").datagrid('load');
			}else{
					alert("修改角色失败！");
			}
		});
	},
	assign:function(){
		var row =$("#role").datagrid('getSelected');
		if(row.length==0){
			alert("请选择一条数据！");
			return;
		}
		getDialog("assignrole").dialog({
			href:contextPath+'/role/assign?roleId='+row.id,
			title: '新增',
		    width: 550,
		    height: 350,
		    buttons:[{
				text:'保存',
				iconCls:'icon-ok',
				handler:function(){
					models.role.doAssign();
				}
				},{
				text:'取消',
				iconCls:'icon-cancel',
				handler:function(){
					$('#assignrole').dialog('destroy');
				}
			}]
		});
	},
	doAssign:function(){
		var nodes =$("#assignmenu").tree('getChecked',['checked','indeterminate']);
		var roleId=$("#roleid").val();
		var menuIds = new Array();
		for (var i=0; i < nodes.length; i++) {
		 	menuIds[i]= nodes[i].id;
		};
		var param = new Object();
		param.menuIds=menuIds;
		param.roleId=roleId;
		$.post(contextPath+"/role/assign",param,function(data){
			alert("分配权限成功！");
			$('#assignrole').dialog('destroy');
		});
	}
	
});
$(function(){
	models.role.init();
});